import { queryBase } from '@/api/base'
import { fungusOriginData_admin, fungusOriginData_manager } from '@/api/originData'
import { queryEnterprise, getAllEnterprise } from '@/api/enterprise'
import { queryGood_entId } from '@/api/good'
import { queryWarehouse_entId } from '@/api/warehouse'
import store from '@/store'

export const fungusMixin = {
  data() {
    return {
      account: store.getters.token,
      role: store.getters.role,
      siteIds: [],
      ents: []
    }
  },
  methods: {
    fetchData() {
      this.listLoading = true
      if (this.role === '1' || this.role === 1) {
        this.fungusOriginData_manager()
      } else {
        queryBase({ account: this.account })
          .then(response => {
            const site = response.plantation
            this.siteIds = site.map(item => {
              return item.siteId
            })
            this.fungusOriginData_admin(0)
          })
          .catch(error => {
            console.log(error)
          })
      }
      if (this.category === 'fungus_produce' || this.category === 'fungus_storage') {
        this.objOption()
      }
    },
    fungusOriginData_manager() {
      fungusOriginData_manager({ account: this.account })
        .then(async response => {
          await this.formatData(response)
          this.checkData()
          this.listLoading = false
        })
        .catch(error => {
          console.log(error)
        })
    },
    fungusOriginData_admin(count) {
      if (count === this.siteIds.length) {
        this.checkData()
        this.listLoading = false
        return
      }
      fungusOriginData_admin({
        siteId: this.siteIds[count]
      })
        .then(async response => {
          await this.formatData(response)
          this.fungusOriginData_admin(++count)
        })
        .catch(error => {
          console.log(error)
        })
    },
    formatData(response) {
      return new Promise(resolve => {
        switch (this.category) {
          case 'fungus_type': {
            this.fungusTypeList.push(...response.fungusType_s)
            break
          }
          case 'fungus_plant': {
            const fungusType = response.fungusType_s.map(item => {
              return {
                label: item.name,
                value: item.fungusTypeId,
                entId: item.entId,
                siteId: item.siteId,
                placeId: item.placeId
              }
            })
            this.fungusTypeOptions.push(...fungusType)
            this.fungusPlantList.push(...response.plantFungus)
            break
          }
          case 'fungus_irrigate': {
            this.siteOption(response)
            response.irrigate.map(item => {
              this.fungusIrrigateList.push(...item)
            })
            break
          }
          case 'fungus_harvest': {
            this.siteOption(response)
            response.harvest.map(item => {
              this.fungusHarvestList.push(...item)
            })
            break
          }
          case 'fungus_produce': {
            this.harvestOption(response)
            response.cropProduce.map(item => {
              this.cropProduceList.push(...item)
            })
            break
          }
          case 'fungus_storage': {
            this.produceOption(response)
            response.storage.map(item => {
              this.cropStorageList.push(...item)
            })
            break
          }
        }
        resolve()
      })
    },
    checkData() {
      switch (this.category) {
        case 'fungus_plant': {
          if (this.fungusTypeOptions.length === 0) {
            this.fungusTypeOptions.push({
              label: '请先添加菌类品种',
              value: -1
            })
          }
          // this.temp.fungusTypeId = this.fungusTypeOptions[0].value
          break
        }
        case 'fungus_irrigate':
        case 'fungus_harvest': {
          if (this.fungusPlantOptions.length === 0) {
            this.fungusPlantOptions.push({
              label: '请先添加种植信息',
              value: -1
            })
          }
          this.temp.plantId = this.fungusPlantOptions[0].value
          break
        }
        case 'fungus_produce': {
          if (this.fungusHarvestOptions.length === 0) {
            this.fungusHarvestOptions.push({
              label: '请先添加采收信息',
              value: -1
            })
          }
          this.temp.harvestId = this.fungusHarvestOptions[0].value
          break
        }
        case 'fungus_storage': {
          if (this.cropProduceOptions.length === 0) {
            this.cropProduceOptions.push({
              label: '请先添加生产信息',
              value: -1
            })
          }
          this.temp.produceId = this.cropProduceOptions[0].value
          break
        }
      }
    },
    siteOption(response) {
      const fungusPlant = response.plantFungus.map(item => {
        return {
          label: item.plantFungusName,
          value: item.plantFungusId
        }
      })
      this.fungusPlantOptions.push(...fungusPlant)
    },
    harvestOption(response) {
      response.harvest.map(item => {
        item.map(i => {
          this.fungusHarvestOptions.push({
            label: i.harvestName,
            value: i.harvestId
          })
        })
      })
    },
    produceOption(response) {
      response.cropProduce.map(item => {
        item.map(i => {
          this.cropProduceOptions.push({
            label: i.cropProduceName,
            value: i.cropProduceId
          })
        })
      })
    },
    objOption() {
      if (this.role === '0') {
        this.getEntAdmin()
      } else {
        this.getEnt()
      }
    },
    getEntAdmin() {
      getAllEnterprise({
        page: -1,
        limit: -1
      })
        .then(response => {
          this.formatEntData(response)
        })
        .catch(error => {
          console.log(error)
        })
    },
    getEnt() {
      queryEnterprise({
        account: this.account,
        page: -1,
        limit: -1
      })
        .then(response => {
          this.formatEntData(response)
        })
        .catch(error => {
          console.log(error)
        })
    },
    formatEntData(response) {
      console.log(response)
      this.ents = response.enterprise
      if (this.ents.length !== 0) {
        if (this.ziduan === 'good') {
          this.getGood(0)
        } else if (this.ziduan === 'warehouse') {
          this.getWarehouse(0)
        }
      }
    },
    getGood(count) {
      if (count === this.ents.length) {
        this.checkGoodOptions()
        this.listLoading = false
        return
      }
      queryGood_entId({ entId: this.ents[count].entId })
        .then(response => {
          const goods = response.good.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
          this.goodOptions.push(...goods)
          this.getGood(++count)
        })
        .catch(error => {
          console.log(error)
        })
    },
    checkGoodOptions() {
      if (this.goodOptions.length === 0) {
        this.goodOptions.push({
          label: '请先添加商品',
          value: -1
        })
      }
      this.temp.goodId = this.goodOptions[0].value
    },
    getWarehouse(count) {
      if (count === this.ents.length) {
        this.checkWarehouseOptions()
        this.listLoading = false
        return
      }
      queryWarehouse_entId({ entId: this.ents[count].entId })
        .then(response => {
          const warehouses = response.Warehouse.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
          this.warehouseOptions.push(...warehouses)
          this.getWarehouse(++count)
        })
        .catch(error => {
          console.log(error)
        })
    },
    checkWarehouseOptions() {
      if (this.warehouseOptions.length === 0) {
        this.warehouseOptions.push({
          label: '请先添加仓库',
          value: -1
        })
      }
      this.temp.warehouseId = this.warehouseOptions[0].value
    }
  }
}
